#include <Arduino.h>

void HX_init();
long ReadCount();

#define HX_SCK 13
#define HX_DT 12
long count0 = 0;
long count = 0;

void setup()
{
    Serial.begin(115200);
    Serial.println("Serial Begin!");
    HX_init();
    Serial.println("HX711_init_done!");
}

void loop()
{
    count = count0 -ReadCount();
    Serial.print("Count0=");
    Serial.print(count0);
    Serial.print("     ");
    Serial.print("Count=");
    Serial.println(count);
    delay(200);
}

void HX_init()
{
    pinMode(HX_SCK, OUTPUT);
    pinMode(HX_DT, INPUT);
    ReadCount();
    delay(100);
    for (int i = 0; i < 8; i++)
        count0 += ReadCount();
    count0 /= 8;
}

long ReadCount()
{
    pinMode(HX_SCK, OUTPUT);
    pinMode(HX_DT, INPUT);
    digitalWrite(HX_SCK, LOW);
    while (digitalRead(HX_DT))
        ;
    unsigned long count_temp = 0;
    for (int i = 0; i < 24; i++)
    {
        digitalWrite(HX_SCK, HIGH);
        count_temp <<= 1;
        digitalWrite(HX_SCK, LOW);
        if (digitalRead(HX_DT))
            count_temp |= 1;
    }
    digitalWrite(HX_SCK, HIGH);
    if (count_temp & 0x00800000)
        count_temp |= 0xFF000000;
    digitalWrite(HX_SCK, LOW);
    return (long)count_temp;
}